package com.sky.service.admin.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.PasswordConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.dto.PasswordEditDTO;
import com.sky.exception.AccountLockedException;
import com.sky.exception.AccountNotFoundException;
import com.sky.exception.EmployeeNameExistException;
import com.sky.exception.PasswordErrorException;
import com.sky.mapper.admin.EmployeeMapper;
import com.sky.pojo.Employee;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.admin.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;

@Service
@Slf4j
public class EmployeeServiceImpl  implements EmployeeService {

    @Resource
    private EmployeeMapper employeeMapper;

    /**
     * 员工登录
     *
     * @param employeeLoginDTO
     * @return
     */
    public Employee login(EmployeeLoginDTO employeeLoginDTO) {
        String username = employeeLoginDTO.getUsername();
        String password = employeeLoginDTO.getPassword();//

        //1、根据用户名查询数据库中的数据  select * from employee where username = #{username}
        Employee employee = employeeMapper.getByUsername(username); //

        //2、处理各种异常情况（用户名不存在、密码不对、账号被锁定）
        if (employee == null) {
            //账号不存在
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }

        //密码比对
        // TODO 后期需要进行md5加密，然后再进行比对
       password=DigestUtils.md5DigestAsHex(password.getBytes(StandardCharsets.UTF_8));
        if (!password.equals(employee.getPassword())) {
            //密码错误
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }

        if (employee.getStatus() == StatusConstant.DISABLE) {
            //账号被锁定
            throw new AccountLockedException(MessageConstant.ACCOUNT_LOCKED);
        }
        //3、返回实体对象
        return employee;
    }

    /**
     * 添加员工
     * @param employeeDTO
     * @return
     */
    @Override
    public Result addEmployee(EmployeeDTO employeeDTO) {
        //去判断用户名是否重复
        Employee employee1 = employeeMapper.getByUsername(employeeDTO.getUsername());
        if (employee1!=null){
          /*  //说明用户名重复
            //这样也是可以
            return Result.error( employee1.getUsername()+"已经存在");*/
            //我们使用异常去抓
            throw new EmployeeNameExistException(employee1.getUsername()+"已经存在");
        }

        //1.先去创建员工对象
        Employee employee=new Employee();
        //2.将dto的属性拷贝到employee对象中
        BeanUtils.copyProperties(employeeDTO,employee);
        //3.给employee其他的值进行赋值
        /*我们采用的是加密的,加密之后的密码*/
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes(StandardCharsets.UTF_8)));
        employee.setStatus(1);

        /*这是默认值*/
        //设置当前记录创建人id和修改人id

        employeeMapper.save(employee);
        System.out.println("employee = " + employee);

        return Result.success();

    }
    /**
     * 员工分页查询
     * @param employeePageQueryDTO
     * @return
     */

    @Override
    public PageResult pageQuery(EmployeePageQueryDTO employeePageQueryDTO) {
        /* 设置分页参数*/
        PageHelper.startPage(employeePageQueryDTO.getPage(),employeePageQueryDTO.getPageSize());

        Page<Employee> page = employeeMapper.pageQuery(employeePageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());

    }

    /**
     * 启用禁用状态
     * @param status
     * @param id
     * @return
     */

    @Override
    public Result startOrStop(int status, int id) {

        int i = employeeMapper.startOrStop(status, id);
        return Result.success("修改成功");

    }

    /**
     * 修改员工的前置,根据id回显
     * @param id
     * @return
     */

    @Override
    public Result findOne(int id) {
        Employee one = employeeMapper.findOne(id);
        return Result.success(one);
    }

    /**
     * 修改
     * @param employee
     * @return
     */

    @Override
    public Result updateEmployee(Employee employee) {

         employeeMapper.updateEmployee(employee);
        return Result.success("修改成功");

    }


}
